#!/usr/bin/env python
import sys
from itertools import groupby

def runlen_enc(inputlist):
    runlen_coded = []
    
    for k,g in groupby(inputlist, lambda x: x):
        runlen_coded.append((len(list(g)), k))

    return runlen_coded

def runlen_dec(runlen_coded):
    rawlist = []
    for (i, s) in runlen_coded:
        for j in range(i):
            rawlist.append(s)
    return rawlist


if __name__ == "__main__":
    coded = runlen_enc([1,2,1,1,4,1])
    print coded
    print runlen_dec(coded)
